package com.sansan.qiangji.realize.device;

import com.sansan.qiangji.VO.device.StackerParamDetailVO;
import com.sansan.qiangji.VO.device.StackerVO;
import com.sansan.qiangji.entity.device.Stacker;
import com.sansan.qiangji.entity.device.StackerParamDetail;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.device.StackerAddForm;
import com.sansan.qiangji.form.device.StackerEditForm;
import com.sansan.qiangji.service.impl.device.StackerParamDetailServiceImpl;
import com.sansan.qiangji.service.impl.device.StackerServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StackerReal {
    private StackerServiceImpl stackerService;
    private StackerParamDetailServiceImpl stackerParamDetailService;

    @Resource
    public void setStackerService(StackerServiceImpl stackerService){
        this.stackerService = stackerService;
    }

    @Resource
    public void setStackerParamDetailService(StackerParamDetailServiceImpl stackerParamDetailService) {
        this.stackerParamDetailService = stackerParamDetailService;
    }

    public List<StackerVO> stackerList(){
        List<StackerVO> stackerVOS = new ArrayList<>();
        List<Stacker> stackers = stackerService.findAllStacker();
        for (Stacker stacker : stackers){
            StackerVO stackerVO = new StackerVO();
            BeanUtils.copyProperties(stacker, stackerVO);
            stackerVOS.add(stackerVO);
        }
        return stackerVOS;
    }

    @Transactional
    public void stackerCreate(StackerAddForm stackerAddForm){
        List<Stacker> stackers = stackerService.findByStackerName(stackerAddForm.getStackerName());
        if (stackers.size()!=0){
            log.error("【添加堆垛机】堆垛机已存在");
            throw new ServiceException(ResultEnum.STACKER_IS_EXIST);
        }
        Stacker stacker = new Stacker();
        BeanUtils.copyProperties(stackerAddForm, stacker);
        stacker.setStackerOnlineStatus(false);
        stackerService.addStacker(stacker);
    }

    @Transactional
    public void stackerEdit(StackerEditForm stackerEditForm){
        Stacker stacker = stackerService.findByStackerId(stackerEditForm.getStackerId());
        if (stacker == null){
            log.error("【编辑堆垛机】堆垛机不存在");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_EXIST);
        }
        BeanUtils.copyProperties(stackerEditForm, stacker);
        stackerService.updateStacker(stacker);
    }

    @Transactional
    public void deleteStacker(Integer stackerId){
        Stacker stacker = stackerService.findByStackerId(stackerId);
        if (stacker == null){
            log.error("【删除堆垛机】堆垛机不存在");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_EXIST);
        }
        stackerService.deleteById(stackerId);
    }

    @Transactional
    public List<StackerParamDetailVO> getStackerParamDetail(Integer stackerId){
        List<StackerParamDetailVO> stackerParamDetailVOs = new ArrayList<>();
        Stacker stacker = stackerService.findByStackerId(stackerId);
        if (stacker == null){
            log.error("【堆垛机参数】堆垛机不存在");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_EXIST);
        }
        stacker.setIsRefreshStackerParam(true);
        stackerService.updateStacker(stacker);
        List<StackerParamDetail> stackerParamDetails = stackerParamDetailService.findByStackerName(stacker.getStackerName());
        for (StackerParamDetail stackerParamDetail : stackerParamDetails){
            StackerParamDetailVO stackerParamDetailVO = new StackerParamDetailVO();
            BeanUtils.copyProperties(stackerParamDetail, stackerParamDetailVO);
            stackerParamDetailVOs.add(stackerParamDetailVO);
        }
        return stackerParamDetailVOs;
    }
}
